﻿using Orchard.ContentManagement.Handlers;
using Orchard.Data;
using Vitus.MediaProcessing.Models;
using Vitus.MediaProcessing.Services;

namespace Vitus.MediaProcessing.Handlers
{
    public class MediaProfileAttachmentsPartHandler : ContentHandler
    {
        private readonly IMediaProfileAttachmentService attachmentService;

        public MediaProfileAttachmentsPartHandler(
            IRepository<MediaProfileAttachmentsPartRecord> attachmentsPartRepository,
            IMediaProfileAttachmentService attachmentService)
        {
            Filters.Add(StorageFilter.For(attachmentsPartRepository));

            this.attachmentService = attachmentService;

            OnRemoved<MediaProfileAttachmentsPart>(RemoveAttachments);
        }

        private void RemoveAttachments(RemoveContentContext context, MediaProfileAttachmentsPart part)
        {
            this.attachmentService.DeleteMediaProfileAttachments(part.Id);
        }
    }
}
